package ua.kharkov.knure.pereverziev.diploma.web.util;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import ua.kharkov.knure.pereverziev.diploma.db.DAOServices.ServiceFactory;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Flight;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Schedule;
import ua.kharkov.knure.pereverziev.diploma.db.entity.WeekDay;

public class FlightsCreator {

	private static final Logger log = Logger.getLogger(FlightsCreator.class);

	public static void createFlightForMonth(final int advanceMonthQuantity) {
		log.trace("createFlightForMonth Started");
		final Float oilPriceRatio = 1f;
		final List<Schedule> schedules = ServiceFactory.getInstance()
				.getScheduleService().getList(new Schedule());
		final Calendar endDate = Calendar.getInstance();
		endDate.add(Calendar.MONTH, advanceMonthQuantity);
		Calendar date = null;
		log.debug("Found " + schedules.size() + "  schedules");
		int i = 0;
		for (final Schedule schedule : schedules) {
			for (final WeekDay day : schedule.getDaysOfWeek()) {
				date = Calendar.getInstance();
				while (date.before(endDate)) {
					if (date.get(Calendar.DAY_OF_WEEK) == day.getId())
						ServiceFactory
								.getInstance()
								.getFlightsService()
								.create(new Flight(schedule, new java.sql.Date(
										date.getTime().getTime()), schedule
										.getRate() * oilPriceRatio));
					date.add(Calendar.DAY_OF_YEAR, 1);
					i++;
				}
			}
		}
		log.trace("createFlightForMonth finished");
	}
}
